#include "../lv_project_inc/lvgl_secure.h"

//发送安全短信通知
void secure_sms()
{
    fd_secure_socket = client_secure_run();
    if(fd_secure_socket == -1)
    {
        perror("client_secure_run error");
        exit(0);
    }
    printf("securefd %d\n",fd_secure_socket);

    char *request = "[request content]";

    int ret = send(fd_secure_socket,request,strlen(request),0);
    printf("secure send ret = %d\n",ret);

    ssize_t size_r = 1024 * 1024;
    char *buf_secure = malloc(size_r);
    memset(buf_secure,0,size_r);

    ret = recv(fd_secure_socket,buf_secure,size_r,0);
    printf("secure recv ret = %d\n",ret);

    printf("secure recv %s\n",buf_secure);

    free(buf_secure);
    buf_secure = NULL;

    close(fd_secure_socket);
}

//初始化烟感
void lvgl_smoke_init()
{
    state_smoke = false;
    lv_img_set_src(img_smoke,&smoke_safe);
}

//初始化警报器
void lvgl_beep_init()
{
    state_beep = false;
    secure_flag = false;

    fd_beep = open("/dev/beep", O_RDWR);
    if(fd_beep == -1)
    {
        perror("beep open error");
        exit(0);
    }
}

//更新安全示意图
void lvgl_secure_update(bool state_s)
{
    if(state_s == true)
    {
        lvgl_lock();
        lv_img_set_src(img_smoke,&smoke_danger);
        lvgl_unlock();
    }
    else
    {
        lvgl_lock();
        lv_img_set_src(img_smoke,&smoke_safe);
        lvgl_unlock();
    }
}

//危险事件
void secure_beep_danger()
{   
    if(secure_flag)
        return;

    int cnt = 10;
    
    secure_sms();
 
    while(cnt--)
    {
        ioctl(fd_beep,BEEP_ON,1);
        usleep(50000);
        ioctl(fd_beep,BEEP_OFF,1);

        usleep(500000);

        ioctl(fd_beep,BEEP_ON,1);
        usleep(50000);
        ioctl(fd_beep,BEEP_OFF,1);
    }
    
    secure_flag = true;
}

//烟感
void lvgl_layout_smoke(lv_obj_t *obj)
{
    lv_obj_set_size(obj,100,100);
    lv_obj_align(obj,LV_ALIGN_BOTTOM_RIGHT,-25,-25);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);

    img_smoke = lv_img_create(obj);
    lv_obj_align(img_smoke,LV_ALIGN_CENTER,0,0);

    lvgl_smoke_init();
    lvgl_beep_init();
}

//紧急呼叫事件处理
void lvgl_call_event_handle(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    if(code == LV_EVENT_CLICKED)
    {
        fd_secure_socket = client_secure_run();
        if(fd_secure_socket == -1)
        {
            perror("client_secure_run error");
            exit(0);
        }
        printf("securefd %d\n",fd_secure_socket);

        char *request = "POST /api/sms/smsoto.htm?"
                        "appkey=1937302854202406231837158525100"
                        "&appsecret=4d6936539d33d2e37d533272aa2f945b"
                        "&smsSignId=debae67aabb74082a5268c0275a7cf26"
                        "&templateId=b526552b86fc4631976d7d77a56babf8"
                        "&mobile=13316322875 HTTP/1.1\r\n"
                        "Host:api.guoyangyun.com\r\n\r\n";;

        int ret = send(fd_secure_socket,request,strlen(request),0);
        printf("call send ret = %d\n",ret);

        ssize_t size_r = 1024 * 1024;
        char *buf_call = malloc(size_r);
        memset(buf_call,0,size_r);

        ret = recv(fd_secure_socket,buf_call,size_r,0);
        printf("call recv ret = %d\n",ret);

        printf("call recv %s\n",buf_call);

        free(buf_call);
        buf_call = NULL;

        close(fd_secure_socket);
    }
}

//紧急呼叫
void lvgl_layout_call(lv_obj_t *obj)
{
    lv_obj_set_size(obj,100,100);
    lv_obj_align(obj,LV_ALIGN_BOTTOM_RIGHT,-125,-25);
    lv_obj_set_style_pad_all(obj,0,0);
    lv_obj_set_style_border_width(obj,0,0);
    lv_obj_set_style_bg_color(obj,lv_color_hex(0x4F4F4F),0);
    lv_obj_add_event_cb(obj,lvgl_call_event_handle,LV_EVENT_CLICKED,NULL);

    img_call = lv_img_create(obj);
    lv_obj_align(img_call,LV_ALIGN_CENTER,0,0);
    lv_img_set_src(img_call,&call);
}
